package com.shangyitong.yygh_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangyitong.ResultCodeEnum;
import com.shangyitong.exception.UserException;
import com.shangyitong.utils.JwtHelper;
import com.shangyitong.yygh.enums.AuthStatusEnum;
import com.shangyitong.yygh.model.acl.User;
import com.shangyitong.yygh.model.user.UserInfo;
import com.shangyitong.yygh.vo.user.LoginVo;
import com.shangyitong.yygh.vo.user.UserAuthVo;
import com.shangyitong.yygh.vo.user.UserInfoQueryVo;
import com.shangyitong.yygh_user.mapper.UserInfoMapper;
import com.shangyitong.yygh_user.service.UserInfoService;
import org.apache.poi.hssf.record.UserSViewEnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Map<String, Object> login(LoginVo loginVo) {
        //获取loginVo中属性
        String phone = loginVo.getPhone();
        String code = loginVo.getCode();
        //判断手机号是否为空或验证码是否为空
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            throw new UserException(ResultCodeEnum.DATA_ERROR);
        }
        //与缓存中验证码进行校对
        String redisCode = stringRedisTemplate.opsForValue().get(phone);
        if (!code.equals(redisCode)) {
            throw new UserException(ResultCodeEnum.CODE_ERROR);
        }
        //判断是否有该手机号
        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("phone", phone));
        //若该手机号为第一次登录，则注册
        if (null == userInfo) {
            //创建对象
            userInfo = new UserInfo();
            //设置用户属性
            userInfo.setPhone(phone);
            userInfo.setStatus(1);
            userInfo.setCreateTime(new Date());
            userInfo.setUpdateTime(new Date());
            //保存数据到数据库
            userInfoMapper.insert(userInfo);
        }
        //查看手机号是否被禁用
        if (userInfo.getStatus() == 0) {
            throw new UserException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }
        //生成token
        String token = JwtHelper.createToken(userInfo.getId(), userInfo.getPhone());
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("name", userInfo.getPhone());
        userMap.put("token", token);
        return userMap;
    }

    @Override
    public void userAuth(Long userId, UserAuthVo userAuthVo) {
        if (userId == null) {
            throw new UserException(ResultCodeEnum.DATA_ISNULL);
        }
        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("id", userId));
        if (userInfo == null) {
            throw new UserException(ResultCodeEnum.DATA_ISNULL);
        }
        if (userInfo.getStatus() == 0) {
            throw new UserException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }
        if (!StringUtils.isEmpty(userAuthVo.getName())) {
            userInfo.setName(userAuthVo.getName());
        }
        if (!StringUtils.isEmpty(userAuthVo.getCertificatesType())) {
            userInfo.setCertificatesType(userAuthVo.getCertificatesType());
        }
        if (!StringUtils.isEmpty(userAuthVo.getCertificatesNo())) {
            userInfo.setCertificatesNo(userAuthVo.getCertificatesNo());
        }
        if (!StringUtils.isEmpty(userAuthVo.getCertificatesUrl())) {
            userInfo.setCertificatesUrl(userAuthVo.getCertificatesUrl());
        }
        userInfo.setAuthStatus(AuthStatusEnum.AUTH_RUN.getStatus());
        userInfoMapper.updateById(userInfo);
    }
}
